import React, { Component } from 'react';
import {
    SnippetsFilled
} from '@ant-design/icons';
import { Link } from 'react-router-dom'
import axios from '../../../axios/axios'
import { message } from 'antd';
class address extends Component {
    constructor(props) {
        super(props);
        this.state = {
            addressNum: 0,
            addressList:[]
        }
    }
    componentWillMount() {
        axios.get('small/user/verify/v1/receiveAddressList').then(res => {
            // console.log(res)
            this.setState({
                addressNum: res.data.result.length,
                addressList:res.data.result
            });
        });
    }
    setadmin(id){
        const params = new FormData();
      params.append("id", parseInt(id));
      axios
        .post("small/user/verify/v1/setDefaultReceiveAddress", params)
        .then((res) => {
          // console.log(res)
          message.success(res.data.message);
          axios.get('small/user/verify/v1/receiveAddressList').then(res => {
            // console.log(res)
            this.setState({
                addressNum: res.data.result.length,
                addressList:res.data.result
            });
        });
        });
    }
    del(id){
        axios.delete('small/user/verify/v1/deleteReceiveAddress?id='+id).then(res=>{
            // console.log(res)
            message.success(res.data.message);
            axios.get('small/user/verify/v1/receiveAddressList').then(res => {
                // console.log(res)
                this.setState({
                    addressNum: res.data.result.length,
                    addressList:res.data.result
                });
            });
        })
    }
    render() {
        return (<div className="address">
            <div className="right-mark">收货地址
                <Link to="/people/addAddress" className="add-btn">添加新地址</Link></div>
            <div className="address-list">

                {this.state.addressNum == 0 ? (<div className="empty-desc"><SnippetsFilled className="icon" />暂无收货地址，请点击
                    <Link to={{pathname:'/people/addAddress',query:{type:1}}}>添加新地址</Link></div>) : (
                        <table cellPadding="0" cellSpacing="0">
                            <thead>
                            <tr>
                                <th className="order">序号</th>
                                <th className="person">收货人</th>
                                <th className="area">所在区域</th>
                                <th className="address">街道地址</th>
                                <th>联系电话</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {this.state.addressList.map((v,i)=>{
                                return(<tr key="v.id">
                                    <td>{i+1}</td>
                                    <td>{v.realName}</td>
                                    <td className="area">{v.address.split(' ')[0]+v.address.split(' ')[1]}</td>
                                    <td className="address">{v.address.split(' ')[2]+v.address.split(' ')[3]}</td>
                                    <td> {v.phone} </td>
                                    <td className="do">
                                        {v.whetherDefault==2&&(<Link className="moren" to="###" onClick={this.setadmin.bind(this,v.id)}>设为默认</Link>)}
                                    <Link to={{pathname:'/people/addAddress',query:{type:0,id:v.id,address:v.address.split(' ')[2]+v.address.split(' ')[3],phone:v.phone,name:v.realName}}}>修改</Link> |
                                     <Link to="###" onClick={this.del.bind(this,v.id)} className="del" val="852">删除</Link>
                                    </td>
                                </tr>)
                            })}

                            </tbody>
                        </table>
                    )}

            </div>
        </div>
        );
    }
}

export default address;